package modeles;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class InfoStock {
	int infoStock_id;
	String infoStock_code;
	String infoStock_date;
	String infoStock_produit_code;
	String infoStock_produit_libelle;
	String infoStock_categ_libelle;
	int stock_id;
	String stock_code;
	String stock_nom;
	String magasinier;
	
	int produit_id;
	int qte;
	
	public InfoStock(){
		
	}

	public int getInfoStock_id() {
		return infoStock_id;
	}

	public void setInfoStock_id(int infoStock_id) {
		this.infoStock_id = infoStock_id;
	}

	public String getInfoStock_code() {
		return infoStock_code;
	}

	public void setInfoStock_code(String infoStock_code) {
		this.infoStock_code = infoStock_code;
	}

	public String getInfoStock_date() {
		return infoStock_date;
	}

	public void setInfoStock_date(String infoStock_date) {
		this.infoStock_date = infoStock_date;
	}

	public int getStock_id() {
		return stock_id;
	}

	public void setStock_id(int stock_id) {
		this.stock_id = stock_id;
	}

	public int getProduit_id() {
		return produit_id;
	}

	public void setProduit_id(int produit_id) {
		this.produit_id = produit_id;
	}

	public int getQte() {
		return qte;
	}

	public void setQte(int qte) {
		this.qte = qte;
	}

	public String getInfoStock_produit_code() {
		return infoStock_produit_code;
	}

	public void setInfoStock_produit_code(String infoStock_produit_code) {
		this.infoStock_produit_code = infoStock_produit_code;
	}

	public String getStock_code() {
		return stock_code;
	}

	public void setStock_code(String stock_code) {
		this.stock_code = stock_code;
	}
	
	

	public String getInfoStock_produit_libelle() {
		return infoStock_produit_libelle;
	}

	public void setInfoStock_produit_libelle(String infoStock_produit_libelle) {
		this.infoStock_produit_libelle = infoStock_produit_libelle;
	}

	public String getInfoStock_categ_libelle() {
		return infoStock_categ_libelle;
	}

	public void setInfoStock_categ_libelle(String infoStock_categ_libelle) {
		this.infoStock_categ_libelle = infoStock_categ_libelle;
	}
	
	

	public String getStock_nom() {
		return stock_nom;
	}

	public void setStock_nom(String stock_nom) {
		this.stock_nom = stock_nom;
	}

	
	
	public String getMagasinier() {
		return magasinier;
	}

	public void setMagasinier(String magasinier) {
		this.magasinier = magasinier;
	}

	public int getNumNextBE(DBManager _dbManager){
		int nextNumBE = 0;
		String zQuery = "SELECT DISTINCT BE_CODE FROM \"BON_ENTREE\"" ;

		try
		{
			Statement oStmt = _dbManager.oStockDbCnx.createStatement () ;	
			ResultSet oRs = oStmt.executeQuery (zQuery) ;
			while (oRs.next ())
			{
				nextNumBE += 1 ;
			}
		}
		catch (SQLException _e)
		{
			System.out.println ("Erreur interne pendant la requÃªte !") ;
			_e.printStackTrace () ;
		}
		return (nextNumBE +1); 
	}
	 public String NVLSqlStr(String objet){
	    	String result = "";
	    	result = "'"+objet+"'";
	    	return result;
	    }
	
	public boolean storeBE(DBManager _dbManager){
		boolean bRes = true;
		
		String zQuery = "INSERT INTO \"BON_ENTREE\" (be_id,be_code,stock_id,produit_id,qte_entree,nom_magasinier) VALUES (nextval('seq_id_be'), " ;
  		zQuery += NVLSqlStr(this.infoStock_code) + ", " ;
  		zQuery += this.getStock_id() + ", " ;
  		zQuery +=  this.getProduit_id() + ", " ;
  		zQuery += this.getQte() + ", " ;
  		zQuery += NVLSqlStr(this.getMagasinier()) + ") " ;
  		
  		try
  		{
  			Statement oStmt = _dbManager.oStockDbCnx.createStatement () ;	
  			oStmt.executeUpdate (zQuery) ;
  		}
  		catch (SQLException _e)
  		{
  			System.out.println ("Erreur interne pendant la mise Ã  jour !") ;
  			bRes = false;
  			_e.printStackTrace () ;
  		}

  		return bRes;
	}
	
	public boolean storeProductToStock(DBManager _dbManager){
		boolean bRes = true;
		
		//verifier si le produit existe dans le stock
		int sumQte = checkProductInStock(_dbManager);
		//si existe alors mise a jour
		if(sumQte>0){
			int newQte = sumQte + this.getQte();
			String zQuery = "UPDATE \"INFO_STOCK\" set qte_stock = " +newQte+ " where stock_id = "+this.getStock_id()+" and produit_id = "+this.getProduit_id();
	  		try
	  		{
	  			Statement oStmt = _dbManager.oStockDbCnx.createStatement () ;	
	  			oStmt.executeUpdate (zQuery) ;
	  		}
	  		catch (SQLException _e)
	  		{
	  			System.out.println ("Erreur interne pendant la mise Ã  jour !") ;
	  			bRes = false;
	  			_e.printStackTrace () ;
	  		}
		}
		else{
			String zQuery = "INSERT INTO \"INFO_STOCK\" (info_stock_id,stock_id,produit_id,qte_stock) VALUES (nextval('seq_id_info_stock'), " ;
	  		zQuery += this.getStock_id() + ", " ;
	  		zQuery +=  this.getProduit_id() + ", " ;
	  		zQuery += this.getQte() + ") ";
	  		try
	  		{
	  			Statement oStmt = _dbManager.oStockDbCnx.createStatement () ;	
	  			oStmt.executeUpdate (zQuery) ;
	  		}
	  		catch (SQLException _e)
	  		{
	  			System.out.println ("Erreur interne pendant la mise Ã  jour !") ;
	  			bRes = false;
	  			_e.printStackTrace () ;
	  		}
		}
  		
  		

  		return bRes;
	}
	
	//verifier si le stock contient deja le produit
	public int checkProductInStock(DBManager _dbManager){
		int bRes = 0;
		String zQuery = "SELECT SUM(qte_stock) as qte FROM \"INFO_STOCK\" where stock_id = "+this.getStock_id()+" and produit_id = "+this.getProduit_id();

		
		try
		{
			Statement oStmtStock = (Statement) _dbManager.oStockDbCnx.createStatement () ;	
			ResultSet oRsProduit = (ResultSet) oStmtStock.executeQuery (zQuery) ;
			if (oRsProduit.next ())
			{
				bRes = oRsProduit.getInt("qte");
			}
		}
		catch (SQLException _e)
		{
			System.out.println ("Erreur interne pendant la requete !") ;
			_e.printStackTrace () ;
		}
		return bRes;
	}
	
	//liste des numeros Bon Entree
	public Vector<String> getListNumBe(DBManager _dbManager){
		Vector vectRes = new Vector <String>();
		
		String zQuery = "SELECT DISTINCT BE_CODE FROM \"BON_ENTREE\" order by BE_CODE ASC" ;

		try
		{
			Statement oStmt = _dbManager.oStockDbCnx.createStatement () ;	
			ResultSet oRs = oStmt.executeQuery (zQuery) ;
			while (oRs.next ())
			{
				String be_code = oRs.getString("BE_CODE");
				vectRes.add(be_code);
			}
		}
		catch (SQLException _e)
		{
			System.out.println ("Erreur interne pendant la requÃªte !") ;
			_e.printStackTrace () ;
		}
		return (vectRes); 
	}
	
}
